package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;

import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.universe.Universe;
import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.DIRECTOR)
public class MoveUM {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private StoreService storeService;
    
    @Property
    private UniverseManager manager;
    
    @Property
    private Long managerId;
    
    @Property
    private Universe universe;
    
    @Property
    private List<Universe> universes;
    
    @Component
    private Form moveUMForm;

    @Inject 
    private PageRenderLinkSource pageLinkSource;
    
    
    void onActivate(Long id) {
    	try {
       		
    		manager = userService.findUniverseManager(id);
    		managerId = id;
    		universe = manager.getUniverse();
    		
    		universes = new ArrayList<Universe>();
    		universes.addAll(manager.getStore().getUniverses());
    		
			} catch (InstanceNotFoundException e) {
		}
    }

    Object onValidateFromMoveUMForm() throws InstanceNotFoundException {
    	
        if (!moveUMForm.isValid()) {
            return this;
        }
        
        manager = userService.findUniverseManager(managerId);
        userService.moveUniverseManager(manager.getUserProfileId(), universe.getUniverseId());
        
        return pageLinkSource.createPageRenderLinkWithContext("user/DisplayUMProfile", managerId);
        
    }
    
    public ValueEncoder<Universe> getUniverseEncoder() {

        return new ValueEncoder<Universe>() {

            @Override
            public String toClient(Universe value) {
                return String.valueOf(value.getUniverseId()); 
            }

            @Override
            public Universe toValue(String id) { 
            	
            	Universe universe = new Universe();
                try {
					universe = storeService.findUniverse(Long.parseLong(id));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstanceNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                
                return universe;
            }
        };
        
    }

}
